<?php

if(STAINLESS != 1){
	//require_once('config.php');
	//redirect("$home_url/");
}

class connect_db{
    protected $host = '';
    protected $user = '';
    protected $pass = '';
    protected $db_name = '';
    protected $cn = '';
    protected $result = '';
    
    public function connect(){
        $this->cn = mysql_connect($this->host,$this->user,$this->pass);
        mysql_select_db($this->db_name,$this->cn);
        mysql_query('set names utf8',$this->cn);
    }
    public function disconnect(){
        if($this->cn){
            mysql_close($this->cn);
        }
    }
    public function query($sql){
        if($sql){
            $this->result = mysql_query($sql);
            if(!$this->result){
                die('không thực thi được truy vấn' . mysql_error());
            }
        }else{
            die('câu truy vấn không được rỗng');
        }
    }
    public function num_rows(){
        if($this->result){
            return mysql_num_rows($this->result);
        }else{
            return 0;
        }
    }
    public function fetch(){
        if($this->result){
            return mysql_fetch_assoc($this->result);
        }
    }
}
class models extends connect_db{
    public $select  = '*';
    public $where   = '';
    public $order   = '';
    public $limit   = '';
    public $join    = '';
    public $search  = '';
	public $groupby = '';
    
    public function __construct($host,$user,$pass,$db_name){
		$this->host		= $host;
		$this->user		= $user;
		$this->pass		= $pass;
		$this->db_name	= $db_name;
        $this->connect();
    }
    public function __destruct(){
        $this->disconnect();
    }
	public function select($select = '*'){
		$this->select=$select;
	}
    public function join($join){
        $this->join = $join;
    }
	public function where($where){
		if(!empty($where)){
			if(is_array($where)){
				foreach($where as $k => $v){
					$sql[] = "$k='$v'";
				}
				$this->where = 'where ' . implode(" and ",$sql);
			}else $this->where = 'where ' . $where;
		}
    }
	public function groupby($groupby){
        if($groupby) $this->groupby = "group by $groupby";
    }
	public function order($order){
        $this->order = "order by $order";
    }
	public function limit($limit){
        if($limit) $this->limit = "limit $limit";
    }
    public function search($key){
        $this->search = $key;
    }	
	public function get_data($table){
        $select = $this->select;
        $join = $this->join != '' ? $this->join : '';
		$where = !empty($this->where) ? $this->where : '';
		$order = $this->order != '' ? $this->order : '';
		$groupby = $this->groupby != '' ? $this->groupby : '';
		$limit = $this->limit != '' ? $this->limit : '';
        $sql = "select $select from $table $join $where $groupby $order $limit";
        //show($sql);
		$this->query($sql);
		return $this->num_rows() > 0 ? $this->fetch_all() : null;
    }
	public function count_rows($table){
        $join = $this->join != '' ? $this->join : '';
		$where = !empty($this->where) ? $this->where : '';
		$order = $this->order != '' ? $this->order : '';
		$groupby = $this->groupby != '' ? $this->groupby : '';
		$limit = $this->limit != '' ? $this->limit : '';
        $sql = "select COUNT(*) as count from $table $join $where $groupby $order $limit";
        //show($sql);
		$this->query($sql);
		return $this->fetch();
    }
    public function fetch_all(){
        while($row = $this->fetch()){
            $data[] = $row;
        }
        return $data;
    }
	public function reset_data(){
		$this->select = '*';
		$this->where   = '';
		$this->groupby  = '';
		$this->order   = '';
		$this->limit   = '';
		$this->join    = '';
		$this->search  = '';
	}
    public function insert($table, $data){
		$col=implode(",",array_keys($data));
		$arr=array_values($data);
		foreach($arr as $arr2){
			$arr3[]="'$arr2'";
		}
		$values=implode(",",$arr3);
		$sql="insert into $table($col) values($values)";
		//show($sql);
		$this->query($sql);
	}
	public function update($table,$data){
		if($this->where != ""){
		
			$where=$this->where;
			foreach($data as $k=>$v){
				$arr[]="$k = '$v'";
			}
			
			$str=implode(" , ",$arr);
			$sql="update $table set $str $where";
			$this->query($sql);
		}
	}
	public function delete($table){
		if($this->where != ""){
			$where=$this->where;
			$sql="delete from $table $where";
			$this->query($sql);
		}
	}
}//end class models

class pages extends models{
    protected $total_record = 0;
    protected $total_page   = 0;
    protected $next      	= '';
    protected $prev      	= '';
    protected $list      	= '';
    protected $segment   	= 5;
    protected $view      	= 5;
    protected $first     	= '';
    protected $last      	= '';
	protected $keyword   	= '';
	protected $current_page	= '1';
	protected $link			= '';
    
    public function __construct($host,$user,$pass,$db_name){
        parent::__construct($host,$user,$pass,$db_name);
    }
    public function __destruct(){
        
    }
	
	public function setLink($link){
        $this->link = $link;
    }
	
	public function setCurrentPage($current_page){
        $this->current_page = $current_page;
    }
	
	public function setTotalRecords($total_record){
		
        $this->total_record = $total_record;
		$this->total_page	= ceil($this->total_record/($this->view + $this->segment));
    }
	
	public function setKeyword($keyword){
        $this->keyword = $keyword;
    }
	
	public function setViewPerPage($view){
        $this->view = $view;
    }
    
    public function getTotalRecord(){
        return $this->total_record;
    }
	public function getTotalPage(){
        return $this->total_page;
    }
    public function getNextPage(){
        return $this->next;
    }
    public function getPreviousPage(){
        return $this->prev;
    }
    public function getFirstPage(){
        return $this->first;
    }
    public function getLastPage(){
        return $this->last;
    }
    public function getListPage(){
        return $this->list;
    }
    public function getViewPerPage(){
        return $this->view;
    }
    public function getSegment(){
        return $this->segment;
    }

    public function setListPage($router,$from){
		if($this->keyword != '')
			$search = "?q=".$this->keyword;
		else
			$search = "";
		if($this->total_page > 1){
			if($this->current_page > 1){
				$this->first = "<li><a href='".$this->link."/page-1$search' title='trang đầu'><i class='fa fa-angle-double-left'></i></a></li>";
				$this->prev = "<li><a href='".$this->link."/page-" . ($this->current_page-1) . "$search' title='trang " . ($this->current_page-1) . "'><i class='fa fa-angle-left'></i></a></li>";
			}

			$begin = $this->current_page - $this->segment;
			if($begin <= 0) $begin = 1;
			$end = $this->current_page + $this->segment;
			if($end > $this->total_page) $end = $this->total_page;
			
			for($i=$begin;$i<=$end;$i++){
				if($i == $this->current_page){
					$this->list[] = "<li class='active'><a>$i</a></li>";
				}else{
					$go = ($i - 1)*$this->view;
					$this->list[] = "<li><a href='".$this->link."/page-$i$search' title='trang $i'>$i</a></li>";
				}
			}

			if($this->current_page < $this->total_page){
				$go = $from + $this->view;
				$this->next = "<li><a href='".$this->link."/page-" . ($this->current_page+1) . "$search' title='trang " . ($this->current_page+1) . "'><i class='fa fa-angle-right'></i></a></li>";
				//if($this->total_record%$to) $go = floor($this->total_record/$to) * $to;
				//else $go = floor($this->total_record/$to) * $to - $to;
				$go = ($this->total_page - 1) * $this->view;
				$this->last = "<li><a href='".$this->link."/page-".$this->total_page."$search' title='trang cuối'><i class='fa fa-angle-double-right'></i></a></li>";
			}
		}
	}
}//end class pages

//function popular
/* function load_view($url,$data = ''){
    require("views/$url.php");
} */